package com.zl.rbac.web.controller;

import com.zl.rbac.VO.ResultCode;
import com.zl.rbac.VO.ResultVO;
import com.zl.rbac.exception.RbacException;
import com.zl.rbac.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @Autowired
    private IUserService userService;

    @PostMapping("login")
    @ResponseBody
    public ResultVO<String> login(String username, String password)  throws Exception{
        try{
            userService.login(username,password);
            return ResultVO.SUCCESS();
        }catch(RbacException e){
            return  new ResultVO<String>(ResultCode.LOGIN_FAILED,e.getMessage());
        }catch(Exception e){
            throw e;
        }
    }


    @RequestMapping("index")
    public String index()  throws Exception{
        return "main/index";
    }

}
